package mo.offer_version2;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class L21 {

    public int[] exchange(int[] nums) {
        List<Integer> even = new ArrayList<>();
        List<Integer> odd = new ArrayList<>();
        for (int num : nums) {
            if (num % 2 == 0) {
                even.add(num);
            } else {
                odd.add(num);
            }
        }
        odd.addAll(even);
        return odd.stream().mapToInt(Integer::intValue).toArray();
    }

    public int[] exchange2(int[] nums) {
        int i = 0, j = nums.length - 1, tmp;
        while(i < j) {
            while(i < j && (nums[i] & 1) == 1) i++;
            while(i < j && (nums[j] & 1) == 0) j--;
            tmp = nums[i];
            nums[i] = nums[j];
            nums[j] = tmp;
        }
        return nums;
    }

    public static void main(String[] args) {
        L21 test = new L21();
        System.out.println(Arrays.toString(test.exchange(new int[]{1, 2, 3, 4})));
    }

}
